<template>
    <div class="second-dept">
        <el-row :gutter="20">
            <el-col :span="12">
                <div class="grid-content bg-purple">
                    <el-input placeholder="请输入内容" prefix-icon="el-icon-search" size="medium"
                        v-model="deptsSearchWord"></el-input>
                    <div class="emp-list">
                        <el-checkbox-group v-model="checkedDepts" @change="handleCheckedCitiesChange">
                            <el-checkbox v-for="item in secondDepts" :label="item.id"
                                :key="item.id">{{ item.name }}</el-checkbox>
                        </el-checkbox-group>
                    </div>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="grid-content bg-purple emp-choose">
                    <span style="line-height: 40px">已选择的部门：</span>
                    <el-button size="small" style="float: right;" @click="handleClear">清空</el-button>
                    <el-card class="box-card" shadow="never">
                        <!-- v-if="checkedDepts.length > 0" -->
                        <div>
                            <div v-for="item in checkedDeptsData" :key="item.id" class="text item">
                                {{ item.name }}
                                <i class="el-icon-circle-close" @click="delCheckedDepts(item.id)"></i>
                            </div>
                        </div>
                    </el-card>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
export default {
    name: "SecondDept",
    props: {
        secondDepts: Array,
        checkSecondDeptsDataOld: Array,
        value: String,
        secondDeptsOrg: Array
    },
    data () {
        return {
            deptsSearchWord: "",
            // 已选择的部门
            checkedDepts: [],
            checkedDeptsData: []
        };
    },
    watch: {
        deptsSearchWord (val) {
            this.$emit('input', val);
        },
        value (val) {
            this.deptsSearchWord = val;
        },
        updateData () { }
    },
    computed: {
        updateData () {
            let newVal = this.checkedDepts;
            let secondDeptsOrg = this.secondDeptsOrg;
            let arr = [];
            secondDeptsOrg.map(item => {
                newVal.map(i => {
                    if (item.id == i) {
                        arr.push(item);
                    }
                });
            });
            this.$nextTick(function () {
                this.checkedDeptsData = arr;
                this.$emit("change-depts", arr);
            });
        }
    },
    mounted () {
        this.recoverData();
    },
    methods: {
        // 删除选中的二级部门
        delCheckedDepts (delVal) {
            // console.log(delVal);
            this.$nextTick(function () {
                this.checkedDepts = this.checkedDepts.filter(item => {
                    return item != delVal;
                });
            });
        },
        // 多选框选择
        handleCheckedCitiesChange (value) {
            // console.log(value);
            this.checkedDepts = value;
        },
        /**
         * 恢复上次没有点击确定之前的数据
         */
        recoverData () {
            this.$nextTick(function () {
                if (this.checkSecondDeptsDataOld.length) {
                    this.checkedDepts = this.checkSecondDeptsDataOld;
                } else {
                    this.checkedDepts = [];
                }

            });
        },
        /**
         * 清空选择数据
         */
        handleClear () {
            this.checkedDepts = [];
            this.$emit("change-depts", []);
        }
    }
};
</script>

<style lang="less" scoped>
.second-dept {
    .grid-content {
        text-align: left;

        .emp-list {
            position: relative;
            margin-top: 15px;
            width: 100%;
            height: 470px;
            border: 1px solid #dcdcdc;
            border-radius: 3px;
            background: #f8f8f8;
            overflow-y: scroll;
            padding: 5px 10px;
            box-sizing: border-box;

            .el-checkbox {
                display: block;
            }
        }

        .text {
            font-size: 14px;
        }

        .item {
            padding: 10px 0;
        }

        .box-card {
            height: 470px;
            margin: 10px 0px 20px 0px;
            // background: #f8f8f8;
            overflow-y: scroll;

            /deep/ .el-card__body {
                padding: 5px 20px;
            }

            .el-icon-circle-close {
                float: right;
                cursor: pointer;
            }
        }

        /deep/ .el-table .cell {
            white-space: nowrap;
        }
    }

}</style>